<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="关重件名称" prop="keyName" label-width="90px">
        <el-input
          v-model="queryParams.keyName"
          placeholder="请输入关重件名称"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="简码" prop="shortCode" label-width="60px">
        <el-input
          v-model="queryParams.shortCode"
          placeholder="请输入简码"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          clearable
          size="small"
          style="width: 200px"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          clearable
          size="small"
          style="width: 200px"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品型号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品型号"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扫描验证码" prop="scanCode" label-width="90px">
        <el-input
          v-model="queryParams.scanCode"
          placeholder="请输入扫描验证码"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:keys:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:keys:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:keys:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:keys:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="keysList"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="60" align="center" fixed="left" />
      <el-table-column label="关重件主键id" align="center" prop="id" width="140" fixed="left" v-if="false" />
      <el-table-column label="关重件名称" align="center" prop="keyName" width="200" fixed="left" />
      <el-table-column label="简码" align="center" prop="shortCode" width="100" />
      <el-table-column label="扫描验证码" align="center" prop="scanCode" width="120" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="200" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="companyName" width="200" />
      <el-table-column label="产品型号" align="center" prop="itemCode" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="margin-right: 9px"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:keys:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:keys:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改关重件数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="公司名称" prop="companyName">
              <el-input
                v-model="form.companyName"
                placeholder="请输入公司名称"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="关重件编码" prop="keyCode">
              <el-select
                v-model="form.keyCode"
                filterable
                placeholder="请选择关重件编码"
                style="width: 240px"
                @change="Change"
              >
                <el-option
                  v-for="item in MESMATERIALOptions"
                  :key="item.materialCode"
                  :label="item.materialCode"
                  :value="item.materialCode"
                >
                  <span style="float: left">{{ item.materialCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.materialName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="关重件名称" prop="keyName">
              <el-input
                v-model="form.keyName"
                placeholder="请输入关重件名称"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="产品型号" prop="itemCode">
              <el-select
                v-model="form.itemCode"
                placeholder="请选择产品型号"
                style="width: 240px"
              >
                <el-option
                  v-for="dict in productCodeOptions"
                  :key="dict.modelCode"
                  :label="dict.modelDesLocal"
                  :value="dict.modelCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="扫描验证码" prop="scanCode">
              <el-input
                v-model="form.scanCode"
                placeholder="请输入扫描验证码"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                v-model="form.startTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间"
                style="width: 240px"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker
                v-model="form.endTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间"
                style="width: 240px"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listKeys,
    getKeys,
    delKeys,
    addKeys,
    updateKeys,
    exportKeys,
  } from "@/api/base/keys";
  import { listAllMaterial } from "@/api/base/material";
  import { listAllSetModel } from "@/api/parameter/set_model";
  export default {
    name: "Keys",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        //物料下拉
        MESMATERIALOptions: [],
        // 产品型号（产品编码）字典
        productCodeOptions: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 关重件数据表格数据
        keysList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          keyName: undefined,
          shortCode: undefined,
          companyName: undefined,
          itemCode: undefined,
          scanCode: undefined,
          
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          companyName: [{ required: true, message: "公司名称不能为空", trigger: "blur" }],
          keyCode: [{ required: true, message: "请选择关重件编码", trigger: "blur" }],
          keyName: [{ required: true, message: "关重件名称不能为空", trigger: "blur" }],
          itemCode: [{ required: true, message: "产品型号不能为空", trigger: "blur" }],
          scanCode: [{ required: true, message: "验证码不能为空", trigger: "blur" }]
        },
      };
    },
    created() {
      this.getList();
      listAllMaterial().then((res) => {
        this.MESMATERIALOptions = res;
      });
      listAllSetModel().then((res) => {
        if (res.code == 200) {
          this.productCodeOptions = res.data;
        }
      });
    },
    methods: {
      Change(e) {
        // 根据id进行筛选（id是唯一的，如果获取多个，需要自己重新定义筛选条件）
        let item = this.MESMATERIALOptions.filter((options) => {
          return options.materialCode === e;
        });
        this.form.keyName = item[0].materialName;
      },
      /** 查询关重件数据列表 */
      getList() {
        this.loading = true;
        listKeys(this.queryParams).then((response) => {
          this.keysList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加关重件数据";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getKeys(id).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "修改关重件数据";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.id != null) {
              updateKeys(this.form).then((response) => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addKeys(this.form).then((response) => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm(
          '是否确认删除关重件数据编号为"' + ids + '"的数据项?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return delKeys(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function () {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm("是否确认导出所有关重件数据数据项?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(function () {
            return exportKeys(queryParams);
          })
          .then((response) => {
            this.download(response.msg);
          })
          .catch(function () {});
      },
    },
  };
</script>
